package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.BlackList;
import com.zhangkunhao.notes.entity.Do.User;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.BlackListServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 用户黑名单的controller
 *
 * @author 20642
 */
@WebServlet("/blackList/*")
public class BlackListServlet extends BaseDaoServlet {
    private BlackListServiceImpl blackListService = new BlackListServiceImpl();

    public void listOtherUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.通过Session接收用户Id
        Integer id = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        PageBean<User> userPageBean = blackListService.listOtherUsers(id);

        //3.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(userPageBean));
    }

    public void listBlackUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.通过Session接收用户Id
        Integer id = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        PageBean<User> userPageBean = blackListService.listBlackUsers(id);

        //3.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(userPageBean));
    }


    public void deFriend(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.get方式接收数据,乱码解决
        int deFriendId = Integer.parseInt(new String(request.getParameter("deFriendId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        System.out.println("deFriendId:" + deFriendId);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        Message<BlackList> blackListMessage = blackListService.deFriend(userId, deFriendId);

        //3.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(blackListMessage));
    }

    public void cancelDeFriend(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.get方式接收数据,乱码解决
        int cancelDeFriendId = Integer.parseInt(new String(request.getParameter("cancelDeFriendId")
                .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        System.out.println("cancelDeFriendId:" + cancelDeFriendId);

        // 通过Session接收用户Id
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        //2.调用service层,封装返回前端的对象
        Message<BlackList> blackListMessage = blackListService.cancelDeFriend(userId, cancelDeFriendId);

        //3.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(blackListMessage));
    }

}
